Repository navigation
fix: reconnect the websocket and replay subscriptions after a drop - #345
Open
saravanan619 wants to merge 2 commits into
Open
saravanan619 wants to merge 2 commits into
saravanan619 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When the WebSocket connection drops,
WebsocketManagernever reconnects and never tells the caller (#340):run()callsws.run_forever()with noreconnectargument, there are noon_close/on_errorhandlers, andws_readystaysTrueforever, so a latersubscribe()raisesWebSocketConnectionClosedExceptionand existing subscriptions go dark with no error. The docs state the WS server "disconnects periodically and without announcement" and that clients must handle reconnects; this SDK does not.On top of that (#338), even if the socket were re-established,
on_openonly replaysqueued_subscriptions, which are only populated before the first connect, and it never clears the queue. So subscriptions made after connecting are silently never re-sent, while pre-connect subscriptions are re-sent on every reconnect and their callbacks fire once per reconnect (the queue replay re-appends toactive_subscriptions, so one message triggers the callback N times after N reconnects).Fix
run()now callsrun_forever(reconnect=5), so websocket-client re-establishes the connection and re-fireson_open.on_close/on_errorhandlers that setws_ready = False, sosubscribe()queues while disconnected instead of sending into a dead socket.on_opendrainsqueued_subscriptionsbefore replaying them, then re-sends everyactive_subscriptionsentry, so no subscription is lost or duplicated on reconnect.ActiveSubscriptionnow also stores the subscription dict so it can be re-sent.subscribe()no longer raises when the socket drops between thews_readycheck and the send: the subscription is queued and delivered on the nexton_open.send_pingcatchesWebSocketConnectionClosedException, so the ping thread survives the reconnect window instead of dying on the first ping sent while the socket is down.Tests
Added
tests/websocket_manager_reconnect_test.py(7 tests, no network, stubbedWebSocketApp): active subscriptions replayed exactly once on reconnect, callbacks not duplicated after two reconnects, the queue is not replayed twice,on_close/on_errorclearws_readyand queue-then-deliver works,subscribe()while the socket is down queues instead of raising,run()passesreconnect=5, and the ping thread survives a failed send.Verified red-then-green: all 7 new tests fail on unpatched code and pass after the fix. Full suite: 43 passed. Black clean.
Note: the queue-draining part of
on_openoverlaps with #319 (which also drains the queue while fixing duplicate single-subscription queueing); happy to rebase once that lands.Fixes #340
Fixes #338